package com.cmrx.model.controller;

import com.cmrx.model.entity.CaseDetail;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.LoginService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.vo.ResultVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("login")
@Slf4j
@Api(value = "/login",description = "用户相关接口类")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    @ApiOperation(value = "系统内部登录测试接口不做外用只做借口调试")
    @ApiImplicitParam(name = "map",value = "{\"userCard\":\"31010619761204162X\"}")
    public ResultVo selectInvestigationList(@RequestBody Map<String,String> map) {
        String userCard = map.get("userCard");
        if(StringUtils.isEmpty(userCard)){
            return ResultVoUtil.message("userCard为空");
        }
        User user = loginService.loginUser(userCard);
        if(user ==null){
            return ResultVoUtil.message("现勘系统不存在该用户身份证");
        }
        Map<String,String> result = new HashMap<String,String>();
        result.put("token",JwtUtil.enToken(user));
        return ResultVoUtil.success(result);
    }

    @RequestMapping(value = "/select/user",method = RequestMethod.POST)
    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\"}")
    public ResultVo selectUser(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.message("token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.message("用户信息为空");
        }
        return ResultVoUtil.success(user);
    }
}
